<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="科研任务单" v-bind="validateInfos.entrustId">
            <JDictSelectTag v-model:value="formData.entrustId" dictCode="lab_wtemporarily,ls_name,id" :show-choose-option="true" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目名称" v-bind="validateInfos.itemName">
            <a-input v-model:value="formData.itemName" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="实验人员" v-bind="validateInfos.userName">
            <a-input v-model:value="formData.userName" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="备注" v-bind="validateInfos.remark">
            <a-input v-model:value="formData.remark" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
      <div>
        <a-tabs v-model:activeKey="activeKey" :size="size">
          <a-tab-pane key="1" tab="基础信息">
            <LabBasicList @ref="registerBasicList" :itemsId="parmasItemId"></LabBasicList>
          </a-tab-pane>
          <a-tab-pane key="2" tab="实验条件">
            <LabNormList @ref="registerNormList" :itemsId="parmasItemId"></LabNormList>
          </a-tab-pane>
          <a-tab-pane key="3" tab="检测指标">
            <LabTargetList @ref="registerTargetList" :itemsId="parmasItemId"></LabTargetList>
          </a-tab-pane>
          <a-tab-pane key="4" tab="实验人员">
            <LabPerList @ref="registePerList" :itemsId="parmasItemId"></LabPerList>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { createTemp2 } from '../LabWtemporarilyItem.api';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { Form, TabsProps } from 'ant-design-vue';
  import LabBasicList from '/@/views/lab/wscientific_item/components/LabBasicList.vue';
  import LabTargetList from '/@/views/lab/wscientific_item/components/LabTargetList.vue';
  import LabPerList from '/@/views/lab/wscientific_item/components/LabPersonList.vue';
  import LabNormList from '/@/views/lab/wscientific_item/components/LabNormList.vue';

  const registerBasicList = ref();
  const registerNormList = ref();
  const registerTargetList = ref();
  const registePerList = ref();

  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  let parmasItemId = ref('');
  const formData = reactive<Record<string, any>>({
    id: '',
    entrustId: '',
    itemId: '',
    itemName: '',
    basisName: '',
    basisNum: '',
    basisScope: '',
    basisAtta: '',
    bigTypeName: '',
    midTypeName: '',
    team: '',
    userId: '',
    userName: '',
    itemStatus: '',
    itemFinishTime: '',
    entrustType: '',
    remark: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);

  const { resetFields, validate, validateInfos } = useForm(formData, { immediate: true });

  const size = ref<TabsProps['size']>('middle');
  const activeKey = ref('1');

  /**
   * 新增
   */
  function add(data) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, data);
    });
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id != null && model.id != '') {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await createTemp2(formData)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }
  //调弹窗组件中数据reload事件
  function reloadTable(itemId) {
    parmasItemId.value = itemId;
  }

  defineExpose({
    add,
    edit,
    submitForm,
    reloadTable,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }

  // .div {
  //   height: 45px;
  //   border: 1px solid #a8aeb2;
  //   background-color: #d2d5d6;
  //   text-align: center;
  //   font: 1.2em sans-serif;
  //   line-height: 45px
  // }

  // .tr {
  //   height: 50px;
  //   border: 1px solid #a8aeb2;
  // }

  // .th {
  //   width: 25%;
  //   text-align: center;
  //   border: 1px solid #a8aeb2;
  //   font: 1.2em sans-serif;
  // }

  :deep(.ant-tabs-nav) {
    margin: 0 0 1px 0 !important;
  }

  :deep(.jeecg-basic-table .ant-table-wrapper .ant-table-title) {
    min-height: 30px;
    padding: 0 0 1px 0 !important;
  }
</style>
../LabWtemporarilyItem.api